/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:48 )
 */

package org.demo.reglement.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.reglement.bean.Reglement;

public class JpaReglementDAO extends GenericJpaDao<Reglement, Integer> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Reglement
	 */
	public JpaReglementDAO() {
		super(Reglement.class);
	}
	
	/**
	 * Search by Reglement example
	 */
	public List<Reglement> search(final Reglement element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Reglement> criteriaQuery = criteriaBuilder.createQuery(Reglement.class);
			Root<Reglement> from = criteriaQuery.from(Reglement.class);

			List<Predicate> listP = new ArrayList<Predicate>();


			if (CoreUtils.isNotBlank(element.getIdreglement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idreglement"), element.getIdreglement());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdabonnement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idabonnement"), element.getIdabonnement());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdmodereglement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idmodereglement"), element.getIdmodereglement());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getMontantreglement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("montantreglement"), element.getMontantreglement());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getDatereglement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("datereglement"), element.getDatereglement());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getDebutduree())) {
				Predicate predicate = criteriaBuilder.equal(from.get("debutduree"), element.getDebutduree());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getFinduree())) {
				Predicate predicate = criteriaBuilder.equal(from.get("finduree"), element.getFinduree());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getReferencereglement())) {
				Predicate predicate = criteriaBuilder.equal(from.get("referencereglement"), element.getReferencereglement());
				listP.add(predicate);
			}
			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Reglement> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}